<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
  </head>
  <body>
    <div id="app">
      <table class="table">
        <thead>
          <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>头像</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="{name,age,avatar,id} in usersList">
            <td>{{name}}</td>
            <td>{{age}}</td>
            <td>
              <img
                width="50px"
                v-if="avatar"
                :src="`http://127.0.0.1:8090/api/files/_pb_users_auth_/${id}/${avatar}?thumb=100x100`"
              />
              <i v-else>无</i>
            </td>
            <td>
              <button @click="()=>delUser(id)" class="btn btn-danger btn-sm">
                删除
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <script type="module">
      import { createApp, ref, reactive, computed } from "./vue.esm-browser.js";
      import PocketBase from "./pocketbase.es.js";
      const pb = new PocketBase("http://127.0.0.1:8090");
      const app = createApp({
        setup() {
          const usersList = ref([]);
          async function getUsersList() {
            const resultList = await pb.collection("users").getList(1, 50);
            usersList.value = resultList.items;
          }
          getUsersList();

          async function delUser(id) {
            if (confirm("确定删除吗")) {
              await pb.collection("users").delete(id);
              getUsersList();
            }
          }
          return {
            usersList,
            delUser,
          };
        },
      });

      app.mount("#app"); //挂载
    </script>
  </body>
</html>
